package com.kucai.yifaqapp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.activity.DaggerNoteActivityComponent;
import com.kucai.yifaqapp.dagger.module.activity.NoteActivityModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.ResNote;
import com.kucai.yifaqapp.presenter.activity.NoteActivityPresenter;
import com.kucai.yifaqapp.ui.view.NoteActivityView;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.util.PriceUtils;

import java.util.ArrayList;

import static com.kucai.yifaqapp.global.App.getContext;

public class NoteActivity extends BaseActivity<NoteActivityPresenter> implements NoteActivityView {

    TextView titleLeftTv;
    TextView headerTitle;
    ImageView headerButton;
    TextView headerButtonText;
    LinearLayout headerButtonRoot;
    LinearLayout llHeaderRoot;
    RecyclerView notelist;
    SwipeRefreshLayout srl;
    LinearLayout skinbg;
    LinearLayout goback;
    private Adapter adapter;
    private ArrayList<ResNote.DataBean> notedata = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleLeftTv = findViewById(R.id.title_left_tv);
        headerTitle = findViewById(R.id.headerTitle);
        headerButton = findViewById(R.id.headerButton);
        headerButtonText = findViewById(R.id.headerButtonText);
        headerButtonRoot = findViewById(R.id.headerButtonRoot);
        llHeaderRoot = findViewById(R.id.ll_headerRoot);
        notelist = findViewById(R.id.notelist);
        srl = findViewById(R.id.srl);
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getnote(Info.user.token);
            }
        });
        skinbg = findViewById(R.id.skinbg);
        goback = findViewById(R.id.goback);
        headerTitle.setText("即时注单");
        goback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        String a = "https://www";
//        String b = ".baidu.com";
//        mPresenter.getnote2(a+b);
        if (Info.user != null) {
            mPresenter.getnote(Info.user.token);
        }
        notelist.setLayoutManager(new LinearLayoutManager(this));
        //创建适配器
        adapter = new Adapter();
        notelist.setAdapter(adapter);
    }

    @Override
    protected void initDagger() {
        DaggerNoteActivityComponent.builder()
                .noteActivityModule(new NoteActivityModule(this))
                .build().inject(this);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_note;
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }

    @Override
    public void Noteget(ResNote resNote) {
        srl.setRefreshing(false);
        notedata.clear();
        notedata.addAll(resNote.data);
        adapter.notifyDataSetChanged();
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.note_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
//            if (position == 0) {
//                holder.notelin.setVisibility(View.GONE);
//            } else {
            holder.notelin.setVisibility(View.VISIBLE);
            holder.notenum.setText(notedata.get(position).totalNums + "");
            holder.notename.setText(notedata.get(position).gameName);
            holder.notemoney.setText(notedata.get(position).totalMoney + "");
            PriceUtils.format(holder.notemoney);
//            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (notedata.get(position).totalNums == 0) {
                        return;
                    } else {
                        //跳转新的界面
                        Intent intent = new Intent(NoteActivity.this, NoteActivitySecond.class);
                        intent.putExtra("gameid", notedata.get(position).gameId);
                        intent.putExtra("gamename", notedata.get(position).gameName);
                        startActivity(intent);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return notedata.size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView notename, notenum, notemoney;
            LinearLayout notelin;

            MViewHolder(View itemView) {
                super(itemView);
                notename = (TextView) itemView.findViewById(R.id.notename);
                notenum = (TextView) itemView.findViewById(R.id.notenum);
                notemoney = (TextView) itemView.findViewById(R.id.notemoney);
                notelin = (LinearLayout) itemView.findViewById(R.id.notelin);

            }
        }
    }
}

